package com.hcb.micro.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Banner;
import com.hcb.micro.dto.Mall;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.ProductSeller;
import com.hcb.micro.service.IBannerService;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IProductSellerService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("mallBanner")
public class MallBannerController extends BaseControllers{
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	IBannerService bannerService;
	
	@Autowired
	IProductSellerService productSellerService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		
		Mall mall = mallService.selectByMallUuid(manager.getMallUuid()); 
		if(mall != null && !"商盟惠购".equals(mall.getMallName())){
			para.put("mallUuid", mall.getMallUuid());
		}
		
		List<Banner> bannerListAll = bannerService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<Banner> bannerList = bannerService.selectByPara(para);
		
		if(bannerList.size() <= 0){
			json.put("result", "1");
			json.put("description", "轮播图列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Banner b:bannerList){
			Map<String,String> map = new HashMap<>();
			
			map.put("id", b.getId()+"");
			
			String mallName = "";
			Mall bannerMall = mallService.selectByMallUuid(b.getMallUuid());
			if(bannerMall != null){
				mallName = bannerMall.getMallName();
			}
			map.put("mallName", mallName);
			map.put("productId", b.getProductId()+"");
			map.put("image", b.getImage()==null?"":b.getImage());
			map.put("indexImage", b.getIndexImage()==null?"":b.getIndexImage());
			map.put("isDisplay", b.getIsDisplay()+"");
			map.put("orderNumber", b.getOrderNumber()+"");
			
			String type = "";
			if(b.getType() == 1){
				type = "单品";
			}else if(b.getType() == 2){
				type = "广告区";
			}
			
			map.put("type", type);
			map.put("startTime", b.getStartTime()==null?"":sdf.format(b.getStartTime()));
			map.put("endTime", b.getEndTime()==null?"":sdf.format(b.getEndTime()));
			map.put("createTime", b.getCreateTime()==null?"":sdf.format(b.getCreateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取轮播图列表成功");
		json.put("list", list);
		json.put("count", bannerListAll.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("bannerId") == null || "".equals(bodyInfo.get("bannerId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Banner b = bannerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("bannerId")));
		
		if(b == null){
			json.put("result", "1");
			json.put("description", "轮播图不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> map = new HashMap<>();
		map.put("id", b.getId()+"");
		map.put("mallUuid", b.getMallUuid());
		map.put("productId", b.getProductId()+"");
		map.put("desctext", b.getDesctext()==null?"":b.getDesctext());
		map.put("indexDesc", b.getIndexDesc()==null?"":b.getIndexDesc());
		map.put("image", b.getImage()==null?"":b.getImage());
		map.put("indexImage", b.getIndexImage()==null?"":b.getIndexImage());
		map.put("isDisplay", b.getIsDisplay()+"");
		map.put("orderNumber", b.getOrderNumber()+"");
		map.put("startTime", b.getStartTime()==null?"":sdf.format(b.getStartTime()));
		map.put("endTime", b.getEndTime()==null?"":sdf.format(b.getEndTime()));
		map.put("createTime", b.getCreateTime()==null?"":sdf.format(b.getCreateTime()));
		
		String typeString = "";
		if(b.getType() == 1){
			typeString = "单品";
		}else if(b.getType() == 2){
			typeString = "广告区";
		}
		map.put("typeString", typeString);
		map.put("type", b.getType()+"");
		//商城列表
        Map<String,Object> para = new HashMap<>();
		List<Mall> mallList = mallService.selectByPara(para);
        List<Map<String,Object>> list2 = new ArrayList<>();
        for (Mall m: mallList)
        {	
        	Map<String,Object> mallmap = new HashMap<>();
        	if(b.getMallUuid().equals(m.getMallUuid())){
        		mallmap.put("hava", 1);
        	}else{
        		mallmap.put("hava", 0);
        	}
        	mallmap.put("mallUuid", m.getMallUuid());
        	mallmap.put("mallName", m.getMallName());
        	list2.add(mallmap);
        }
        map.put("mallList", list2);
        
        //供应商列表
        Map<String,Object> productSellerPara = new HashMap<>();
		List<ProductSeller> productSellerAllList = productSellerService.selectAllByPara(productSellerPara);
		List<Map<String,Object>> psList = new ArrayList<>();
		if(productSellerAllList.size()>0){
			for (ProductSeller productSeller: productSellerAllList)
	        {	
	        	Map<String,Object> psMap = new HashMap<>();
	        	
	        	if(b.getProductId() == productSeller.getId()){
	        		psMap.put("hava", 1);
	        	}else{
	        		psMap.put("hava", 0);
	        	}
	        	psMap.put("id", productSeller.getId());
	        	psMap.put("name", productSeller.getName());
	        	psList.add(psMap);
	        }
			json.put("productSellerList", psList);
		}
		
    	json.put("result", "0");
		json.put("description", "获取轮播图成功");
		json.put("banner", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("bannerId") == null || "".equals(bodyInfo.get("bannerId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Banner b = bannerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("bannerId")));
		
		if(b == null){
			json.put("result", "1");
			json.put("description", "轮播图不存在");
			return buildReqJsonObject(json);
		}
		
		if(bodyInfo.get("mallUuid") != null && !"".equals(bodyInfo.get("mallUuid"))) {
			b.setMallUuid(bodyInfo.getString("mallUuid"));
		}
		if(bodyInfo.get("type") != null && !"".equals(bodyInfo.get("type"))) {
			b.setType(Byte.valueOf(bodyInfo.getString("type")));
		}
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			b.setProductId(Integer.valueOf(bodyInfo.getString("productId")));
		}
		if(bodyInfo.get("desctext") != null && !"".equals(bodyInfo.get("desctext"))) {
			b.setDesctext(bodyInfo.getString("desctext"));
		}
		if(bodyInfo.get("indexDesc") != null && !"".equals(bodyInfo.get("indexDesc"))) {
			b.setIndexDesc(bodyInfo.getString("indexDesc"));
		}
		if(bodyInfo.get("productSellerId") != null && !"".equals(bodyInfo.get("productSellerId"))) {
			b.setProductId(Integer.valueOf(bodyInfo.getString("productSellerId")));
		}
		if(bodyInfo.get("isDisplay") != null && !"".equals(bodyInfo.get("isDisplay"))) {
			b.setIsDisplay(Byte.valueOf(bodyInfo.getString("isDisplay")));
		}
		if(bodyInfo.get("image") != null && !"".equals(bodyInfo.get("image"))) {
			b.setImage(bodyInfo.getString("image"));
		}
		if(bodyInfo.get("indexImage") != null && !"".equals(bodyInfo.get("indexImage"))) {
			b.setIndexImage(bodyInfo.getString("indexImage"));
		}
		if(bodyInfo.get("orderNumber") != null && !"".equals(bodyInfo.get("orderNumber"))) {
			b.setOrderNumber(Byte.valueOf(bodyInfo.getString("orderNumber")));
		}
		
		b.setStartTime(sdf.parse(bodyInfo.getString("startTime")));
		b.setEndTime(sdf.parse(bodyInfo.getString("endTime")));
		b.setCreateTime(new Date());
		
		Integer rs = bannerService.updateByPrimaryKeySelective(b);
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "修改轮播图失败");
		}else{
			json.put("result", "0");
			json.put("description", "修改轮播图成功");
		}
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws ParseException {
		JSONObject json = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("startTime") == null || "".equals(bodyInfo.get("startTime"))) {
			right = 0;
		}else if(bodyInfo.get("endTime") == null || "".equals(bodyInfo.get("endTime"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Banner b = new Banner();
		
		if(bodyInfo.get("mallUuid") != null && !"".equals(bodyInfo.get("mallUuid"))) {
			b.setMallUuid(bodyInfo.getString("mallUuid"));
		}
		if(bodyInfo.get("type") != null && !"".equals(bodyInfo.get("type"))) {
			b.setType(Byte.valueOf(bodyInfo.getString("type")));
		}
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			b.setProductId(Integer.valueOf(bodyInfo.getString("productId")));
		}
		if(bodyInfo.get("desctext") != null && !"".equals(bodyInfo.get("desctext"))) {
			b.setDesctext(bodyInfo.getString("desctext"));
		}
		if(bodyInfo.get("indexDesc") != null && !"".equals(bodyInfo.get("indexDesc"))) {
			b.setIndexDesc(bodyInfo.getString("indexDesc"));
		}
		if(bodyInfo.get("productSellerId") != null && !"".equals(bodyInfo.get("productSellerId"))) {
			b.setProductId(Integer.valueOf(bodyInfo.getString("productSellerId")));
		}
		if(bodyInfo.get("isDisplay") != null && !"".equals(bodyInfo.get("isDisplay"))) {
			b.setIsDisplay(Byte.valueOf(bodyInfo.getString("isDisplay")));
		}
		if(bodyInfo.get("image") != null && !"".equals(bodyInfo.get("image"))) {
			b.setImage(bodyInfo.getString("image"));
		}
		if(bodyInfo.get("indexImage") != null && !"".equals(bodyInfo.get("indexImage"))) {
			b.setIndexImage(bodyInfo.getString("indexImage"));
		}
		if(bodyInfo.get("orderNumber") != null && !"".equals(bodyInfo.get("orderNumber"))) {
			b.setOrderNumber(Byte.valueOf(bodyInfo.getString("orderNumber")));
		}
		
		b.setStartTime(sdf.parse(bodyInfo.getString("startTime")));
		b.setEndTime(sdf.parse(bodyInfo.getString("endTime")));
		b.setCreateTime(new Date());
		
		Integer rs = bannerService.insertSelective(b);
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "新增轮播图失败");
		}else{
			json.put("result", "0");
			json.put("description", "新增轮播图成功");
			json.put("bannerId", b.getId());
		}
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("bannerId") == null || "".equals(bodyInfo.get("bannerId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Banner b = bannerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("bannerId")));
		
		if(b == null){
			json.put("result", "1");
			json.put("description", "轮播图不存在");
			return buildReqJsonObject(json);
		}
		
		Integer rs = bannerService.deleteByPrimaryKey(b.getId());
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "删除轮播图失败");
		}else{
			json.put("result", "0");
			json.put("description", "删除轮播图成功");
		}
		return buildReqJsonObject(json);
	}
}
